import os
root  = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
project = os.path.basename(root)
print("project name: "+project)
daoRoot = project+"/core/dao"
tablePackage = project+"/core/dao/table"
commonPackage = project+"/core/service/common"
localwrapPackage  = project+"/core/localwrap"

packages = {
	"userdao":["User"],
	"teamdao":["Team"],
}

daoTemplate='''package ${package}

import (
	"${tablePackage}"
	"${commonPackage}"

	"strconv"

	"github.com/RocksonZeta/wrap/redised"
	"github.com/RocksonZeta/wrap/utils/sutil"
	"github.com/RocksonZeta/wrap/utils/timeutil"
)

type ${table}s struct {
	*redised.RedisedMysql
}

func (s ${table}s) redisKey(id int) string {
	return "${table}/" + strconv.Itoa(id)
}

func (s ${table}s) ClearCache(id int) {
	s.Redis.Del(s.redisKey(id))
}

type ${table}sQueryParam struct {
	common.Page
	Name string
}

//Query 根据条件查询
func (s ${table}s) Query(param ${table}sQueryParam) ([]table.${table}, int64) {
	wsql := " from ${table} where 1=1"
	if param.Name != "" {
		param.Name = "%" + param.Name + "%"
		wsql += " and name like :Name"
	}
	ssql := "select *" + wsql + " order by ct desc" + param.Limit()
	csql := "select count(*)" + wsql
	var r []table.${table}
	s.Mysql.Select(&r, ssql, param)
	return r, s.Mysql.SelectInt(csql, param)
}

func (s ${table}s) Get(id int) table.${table} {
	var r table.${table}
	s.RedisedMysql.Get(&r, "${table}", "Id", id)
	return r
}
func (s ${table}s) GetByEmail(email string) table.${table} {
	var r table.${table}
	s.RedisedMysql.GetByKvs(&r, "${table}", "Id", sutil.M{"email": email})
	return r
}

func (s ${table}s) List(ids []int) []table.${table} {
	var r []table.${table}
	s.RedisedMysql.List(&r, "${table}", "Id", ids)
	return r
}

type ${table}sAddParam struct {
	Name string
}

func (s ${table}s) Add(param ${table}sAddParam) table.${table} {
	var t table.${table}
	sutil.Copy(param, &t)
	t.Ct = timeutil.Now()
	s.Mysql.AddTable(t)
	s.Mysql.Insert(&t)
	s.ClearCache(t.Id)
	return t
}

type ${table}sUpdateParam struct {
	Id   int
	Name string
}

func (s ${table}s) Update(param ${table}sUpdateParam) {
	s.Mysql.Patch("${table}", "Id", param)
	s.ClearCache(param.Id)
}

func (s ${table}s) Delete(id int) {
	s.Mysql.Delete("${table}", "Id", id)
	s.ClearCache(id)
}

'''

daoTestTemplate='''package ${package}_test

import (
	"${daoRoot}/${package}"
	"$localwrapPackage"
	"testing"

	"github.com/stretchr/testify/suite"
)
func Test${table}sSuite(t *testing.T) {
	suite.Run(t, new(${table}sSuite))
}
type ${table}sSuite struct {
	suite.Suite
	x *${package}.${table}s
}
func (s *${table}sSuite) SetupSuite() {
}
func (s *${table}sSuite) SetupTest() {
	s.x = &${package}.${table}s{RedisedMysql: localwrap.GetRedisedMysql()}
}
func (s *${table}sSuite) TearDownTest() {
	s.x.Close()
}
func (s *${table}sSuite) TestGet() {
	s.x.Get(1)
}

'''


from string import Template

def write(content , package, file) :
	d = os.path.join(os.path.dirname(root),daoRoot,package)
	if not os.path.exists(d) :
		os.makedirs(d)
	dst = os.path.join(d,file)
	if os.path.exists(dst) :
		print(dst +" has already exists,ignore it")
		return
	print("create "+dst)
	f = open(dst,"w+")
	f.write(content)
	f.close()

def make():
	tpl = Template(daoTemplate)
	testTpl = Template(daoTestTemplate)
	for package,tables in packages.items():
		for table in tables:
			param = {"package":package,"table":table,"daoRoot":daoRoot,"tablePackage":tablePackage,"commonPackage":commonPackage,"localwrapPackage":localwrapPackage}
			dao=tpl.substitute(param)
			write(dao, package , table+"s.go")
			daoTest=testTpl.substitute(param)
			write(daoTest, package , table+"s_test.go")
			

make()